package com.example.chapter05;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;

public class CheckBoxActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_box);
        CheckBox check_custom = findViewById(R.id.check_custom);
        CheckBox check_system = findViewById(R.id.check_system);
        check_custom.setOnCheckedChangeListener(this);
        check_system.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        String desc = String.format("复选框状态：%s", isChecked ? "已选中" : "未选中");
        if (buttonView.getId() == R.id.check_custom) {
            desc = "custom" + desc;
        } else if (buttonView.getId() == R.id.check_system) {
            desc = "system" + desc;
        }
        buttonView.setText(desc);
    }
}